package com.shanghaionstar.login;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.PreferencesUtls;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.joinme.RegisterToVisitorResponse;
import com.onstar.cn.weight.TimeButton;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.login.iview.ISupplePhoneView;
import com.shanghaionstar.login.presenter.SupplementPhonePresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.weight.CommonInputLayout;
import com.shanghaionstar.weight.CommonProgressButton;
import com.shanghaionstar.weight.TopLayout;


/**
 * This Activity is used to supplement phone number
 * while the user hasn't provided phone no before!
 */
@ContentView(R.layout.supplement_phone)
public class SupplementPhoneActivity extends SwipeActivity implements ISupplePhoneView {
    @FindViewById
    CommonInputLayout phone_no_et;
    @FindViewById
    TopLayout top_layout;
    @FindViewById
    CommonInputLayout verify_code_et;
    @FindViewById
    TimeButton get_code_btn;
    @FindViewById
    CommonProgressButton btn_submit;
    SupplementPhonePresenter presenter;
    private boolean isGetCode = false;
    private String idToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new SupplementPhonePresenter(getScheduler(),SupplementPhoneActivity.this);
        presenter.setView(this);
        setListener();
        idToken = PreferencesUtls.getInstance(SupplementPhoneActivity.this).getString(Constants.ID_TOKEN);
        PreferencesUtls.getInstance(SupplementPhoneActivity.this).put(Constants.ID_TOKEN,"");
        ApplicationUtil.getInstance().setAutoLoging(false);
    }


    private void setListener() {
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (presenter.verifyInput(phone_no_et) && presenter.verifyInputCode(verify_code_et)) {
                    BusinessUtil.sendReportByClickType(SupplementPhoneActivity.this, FunctionNameFindId.CompulsoryMobNo_clickSave.getStringValue());
                    isGetCode = false;
                    presenter.setUserInfo(phone_no_et.getText(),verify_code_et.getText());
                    presenter.start();
                }
            }
        });
        get_code_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (presenter.verifyInput(phone_no_et)) {
                    BusinessUtil.sendReportByClickType(SupplementPhoneActivity.this, FunctionNameFindId.CompulsoryMobNo_clickVerificationCode.getStringValue());
                    isGetCode = true;
                    verify_code_et.setText("");
                    presenter.setUserInfo( phone_no_et.getText(), "");
                    presenter.start();
                }
            }
        });

//        top_layout.setOnClickListener(null);
    }



    @Override
    public void setLoading(boolean isLoading) {
        verify_code_et.setEditEnabled(!isLoading);
//        et_new_user_info.setEditEnabled(!isLoading);

        if (CommonUtil.isNull(verify_code_et.getText())) {
            verify_code_et.showLoading(isLoading);
        } else {
            btn_submit.showLoading(isLoading);
        }
    }

    @Override
    public void verifyResult(View view, int errorResId) {
        if (view instanceof CommonInputLayout) {
            CommonInputLayout inputLayout = (CommonInputLayout) view;
            inputLayout.setEnabled(true);
//            inputLayout.setEditEnabled(true);
            if (errorResId == FinalConstants.Register.NO_ERROR) {
                inputLayout.setCheckHintText(null);
                inputLayout.setEditEnabled(false);
            } else if (errorResId == FinalConstants.Register.CHECK_OK) {
                inputLayout.showLoading(false);
                inputLayout.setRightIcon(R.drawable.onstar_joinme_check_ok);
            } else if (errorResId == FinalConstants.Register.CHECK_CANCEl) {
                inputLayout.setRightIcon(-1);
                inputLayout.showLoading(false);
            } else if (errorResId == FinalConstants.Register.SHOW_LOADING) {
                inputLayout.showLoading(true);
            } else {
                inputLayout.setCheck(false);
                inputLayout.setCheckHintText(errorResId);
            }
        }
    }

    @Override
    public void showGaaPhoneNumber(String number) {
        phone_no_et.setText(number==null?"":number);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(SupplementPhoneActivity.this, e);
//        phone_no_et.setEditEnabled(true);
        isGetCode = false;
    }

    @Override
    public void onReponseSuccess(RegisterToVisitorResponse object) {
        if (isGetCode) {
            if (!CommonUtil.isNull(object.getIsMobileUnique()) && object.getIsMobileUnique().equalsIgnoreCase("false")) {
                verifyResult(phone_no_et, R.string.SB022_MSG001);
                phone_no_et.setEditEnabled(true);
                return;
            }
            get_code_btn.start(R.string.again_send);
            isGetCode = false;
        } else {
            PreferencesUtls.getInstance(SupplementPhoneActivity.this).put(Constants.ID_TOKEN,idToken);
            ApplicationUtil.getInstance().setAutoLoging(true);
            AlertDialogUtil.showAlertDialog(SupplementPhoneActivity.this, R.string.dialog_warning,R.string.update_mobile_success, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                    Intent intent=new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
    }



    @Override
    public BasePresenter getPresenter() {
        return presenter;
    }



    @Override
    public Activity onActivity() {
        return this;
    }


}
